using System;
using System.Collections.Generic;
using System.Text;

namespace TuringMachine.StateAutomat
{
    public class State 
    {
        #region Private members
        private String name;
        private StateType type;        
        private Dictionary<char, State> transactionTable = null;
        #endregion

        #region Properties
        public StateType Type
        {
            get { return type; }
            set { type = value; }
        }
        public String Name
        {
            get { return name; }
            set { name = value; }
        }
        #endregion

        #region Constructors
        public State()
        {
            transactionTable = new Dictionary<char, State>();
            name = string.Empty;
            type = StateType.Unknown;
        }
        public State(String name)
        {
            transactionTable = new Dictionary<char, State>();
            name = name;
            type = StateType.Unknown;
        }
        public State(String name, StateType type)
        {
            transactionTable = new Dictionary<char, State>();
            name = name;
            type = type;
        }
        #endregion

        #region Public methods
        public Boolean IsStart()
        {
            return (type == StateType.StartState);
        }
        public Boolean IsHalt()
        {
            return (type == StateType.HaltState);
        }
        public State GetNextState(char inputChar)
        {
            if (transactionTable.ContainsKey(inputChar))
            {
                return transactionTable[inputChar];
            }
            else
            {
                return null;
            }
        }
        public void AddTransition(char inputChar, State nextState)
        {
            if (transactionTable.ContainsKey(inputChar))
            {
                throw new Exception("Already exist transaction from state '" + this.Name + "' with character '" + inputChar + "'");
            }
            else
            {
                transactionTable.Add(inputChar, nextState);
            }
        }        
        #endregion
    }
}
